package cn.yzgy.yzgyoa.ucenter.controller;

import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.StatusCode;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.ucenter.dto.PermissionsFromHandlerDto;
import cn.yzgy.yzgyoa.ucenter.entity.SysPermission;
import cn.yzgy.yzgyoa.ucenter.service.impl.SysPermissionServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import cn.yzgy.yzgyoa.common.utils.ToolsUtil;
import cn.yzgy.yzgyoa.ucenter.service.ISysPermissionService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.yzgy.yzgyoa.common.controller.BaseController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-20
 */
@Syslog
@RestController
@RequestMapping("/ucenter/sys-permission")
public class SysPermissionController extends BaseController {

    private final ISysPermissionService iSysPermissionService;
    private final ToolsUtil toolsUtil;
    private final SysPermissionServiceImpl permissionService;

    public SysPermissionController(SysPermissionServiceImpl permissionService, ISysPermissionService iSysPermissionService, ToolsUtil toolsUtil) {
        this.permissionService = permissionService;
        this.iSysPermissionService = iSysPermissionService;
        this.toolsUtil = toolsUtil;
    }

    @GetMapping("flushAll")
    @ApiDesc("刷新全部权限")
    @RequiresPermissions("api:ucenter:perms:flushAll")
    public Urmf flushAllPermission() {
        return Urmf.success(getCurrentMethodName(), permissionService.flushAllPerms().replace("\n", "<br/>"));
    }

    @GetMapping
    @ApiDesc(value = "读取权限列表")
    @RequiresPermissions("api:ucenter:perms:list")
    public Urmf getPermissions(int page, int size, String permName) {

        final String permNameColumnName = "p_name";
        QueryWrapper<SysPermission> queryWrapper = new QueryWrapper<>();
        if (permName != null) {
            queryWrapper.like(permNameColumnName, permName);
        }
        return Urmf.success(getCurrentMethodName(), permissionService.page(new Page<>(page, size), queryWrapper));
    }

    @GetMapping("sys_permission")
    @ApiDesc(value = "从Handler获取权限列表")
    @RequiresPermissions("api:ucenter:perms:handler_perm_list")
    public Urmf getHandlerPermissions() {
        return Urmf.success(getCurrentMethodName(), permissionService.getPermissionsFromHandler());
    }

    @PostMapping
    @ApiDesc(value = "更新&新增权限", log = true)
    @RequiresPermissions("api:ucenter:perms:add")
    public Urmf updatePermissions(@RequestBody PermissionsFromHandlerDto dto) {
        return Urmf.success(getCurrentMethodName(),permissionService.updateOrAddPermission(dto));
    }
    /**
     * 读取数据，为前端多选下拉提供data来源
     * @return 下拉数据json
     */
    @RequestMapping(value="/permissionsTree/{roleId}")
    @ApiDesc(value = "树状权限查询")
    @RequiresPermissions("api:ucenter:perms:permstree")
    @ResponseBody
    public String permissionsTree(@PathVariable String roleId){
        List<PermissionsFromHandlerDto> permsDtoList = iSysPermissionService.getPermissionsFromHandler();
        for (PermissionsFromHandlerDto permissionsFromHandlerDto : permsDtoList) {
            iSysPermissionService.updateOrAddPermission(permissionsFromHandlerDto);
        }
        JSONObject permsTreeJson=toolsUtil.getReturnData("PermsTree");
        List<Map<String, Object>> permissionsList = permissionService.permissionsTree(roleId);

        permsTreeJson.put("data",permissionsList);
        System.out.println(permsTreeJson.toJSONString());
        return permsTreeJson.toJSONString();
    }

    /**
     * 权限页面修改权限名称
     * @return 下拉数据json
     */
    @PutMapping("/changeInfo")
    @ApiDesc(value = "权限名称修改")
    @RequiresPermissions("api:ucenter:perms:changeinfo_pName")
    @ResponseBody
    public Object changeInfo(@RequestBody Map<String,String> perms){
        int num;
        SysPermission sysPermission = new SysPermission();
        sysPermission.setPName(perms.get("pName"));
        sysPermission.setPVal(perms.get("pVal"));
        num = permissionService.changeInfo(sysPermission);
        if(num<=0){
            return Urmf.fail("修改权限名称", StatusCode.FAILED,num);
        }
        return Urmf.success("修改权限名称", StatusCode.SUCCESS,num);
    }

    /**
     * 专供权限页面调用数据接口使用
     * @return 下拉数据json
     */
    @RequestMapping("/permissionsHtmlTree")
    @ApiDesc(value = "权限页面调用树状权限查询")
    @RequiresPermissions("api:ucenter:perms:permshtmltree")
    @ResponseBody
    public String permissionsHtmlTree(){
        List<PermissionsFromHandlerDto> permsDtoList = iSysPermissionService.getPermissionsFromHandler();
        for (PermissionsFromHandlerDto permissionsFromHandlerDto : permsDtoList) {
            iSysPermissionService.updateOrAddPermission(permissionsFromHandlerDto);
        }
        JSONObject permsJson=toolsUtil.getReturnData("Perms");
        Map<String ,Object> data = new HashMap<>();
        List<Map<String, Object>> permissionsList = permissionService.permissionsTree("");
        data.put("records",permissionsList);
        permsJson.put("data",data);
        System.out.println(permsJson.toJSONString());
        return permsJson.toJSONString();
    }

    /**
     * 读取数据，为前端多选下拉提供data来源
     * @return 下拉数据json
     */
    @RequestMapping("/getPermissionslist")
    @ApiDesc(value = "权限下拉列表查询")
    @RequiresPermissions("api:ucenter:perms:getlist")
    @ResponseBody
    public  String getPermissionslist(){
        JSONObject sysPermissionsJson=toolsUtil.getReturnData("Permissions");
        List<SysPermission> permissions = iSysPermissionService.getPermissionsList();
        for (SysPermission permission : permissions) {
            permission.setTitle(permission.getPName());
            permission.setId(permission.getPId());
            permission.setName(permission.getPName());
            permission.setParentId(permission.getPParent());
            permission.setCheckArr("0");
        }
        sysPermissionsJson.put("data",permissions);
        return sysPermissionsJson.toJSONString();
    }
}
